package com.yx.controller;


import com.yx.entity.SecondhandBook;
import com.yx.service.ISecondhandBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/secondhandBook")
public class SecondhandBookController {
    @Autowired
    private ISecondhandBookService ss;
    @GetMapping("/selectAll")
    public List<SecondhandBook> selectAll(){
        return ss.list();
    }
    @GetMapping("/selectOne/{id}")
    public SecondhandBook selectOne(@PathVariable("id") Integer id){
        return ss.getById(id);
    }
    @PutMapping("/updatePrice")
    public void updatePrice(@RequestBody SecondhandBook shb){
        ss.updateById(shb);
    }
    @PostMapping("/add")
    public void add(@RequestBody SecondhandBook sb){
        ss.save(sb);
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        ss.removeById(id);
    }
    @Value("${attachPath}")
    private String path;
    @PostMapping("/upload")
    public String uploadAttach(MultipartFile pic) throws IOException {
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        String oldName=pic.getOriginalFilename();//获取到上传的文件的原始文件名
        String newName = UUID.randomUUID().toString()+oldName;
        pic.transferTo(new File(path,newName));
        return newName;
    }
}

